$('.today-datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "customerOperation/list",
		"data" : function(d) {
            var query = $("#query1").serializeObject();
            query["size"] = d.length;
            query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
            var json = jQuery.parseJSON( data );
            json.recordsTotal = json.totalElements;
            json.recordsFiltered = json.totalElements;
            json.data = json.content;
 
            return JSON.stringify( json );
        }
	 },
	 "columns": [
         { "data": "customerName" },
         { "data": "distributorName" },
         { "data": "operator" },
         {
             render : function(data,type, row, meta) {
            	 if(row['presentStatus'] == '0'){
            		 return '是';
            	 } else if(row['presentStatus'] == '1'){
            		 return '否';
            	 }else{
            		 return '未知';
            	 }
             }
          },
         { "data": "operationTime" },
         { "data": "address" },
         {
             render : function(data,type, row, meta) {
            	 if(row['status'] == '0'){
            		 return '已操作';
            	 } else if(row['status'] == '1'){
            		 return '未操作';
            	 }else{
            		 return '未知';
            	 }
             }
          },
         { "data": "productName" },
         { "data": "remark" },
         {
            render : function(data,type, row, meta) {
                return $("#btnGroup").html();
            }
         }
     ],
     "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			$("#edit").validate().resetForm();
		    $("#edit").fill(row);
		    
		    $(".select").trigger("change");
		    $("#productId").val(row.productId);
		    $("#productId").trigger("change");
		    
		 });
		 
		 $(".btn-down").click(function(){
				var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
				swal({
					title : "已操作",
					text : "确定已操作?",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "确定",
					cancelButtonText: "取消",
					closeOnConfirm : false
				},function() {
					$.get(contextpath + "customerOperation/down/" + row.id, function(d){
						$('.datatable-basic').DataTable().draw();
						swal("已操作记录成功","", "success");
					})
				});
			 });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "删除操作记录",
				text : "确定要操作记录?",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "customerOperation/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除操作记录成功","", "success");
				})
			});
		 });
      }
});

$('.tomorrow-datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "customerOperation/list",
		"data" : function(d) {
           var query = $("#query2").serializeObject();
           query["size"] = d.length;
           query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
           var json = jQuery.parseJSON( data );
           json.recordsTotal = json.totalElements;
           json.recordsFiltered = json.totalElements;
           json.data = json.content;

           return JSON.stringify( json );
       }
	 },
	 "columns": [
        { "data": "customerName" },
        { "data": "distributorName" },
        { "data": "operator" },
        {
            render : function(data,type, row, meta) {
           	 if(row['presentStatus'] == '0'){
           		 return '是';
           	 } else if(row['presentStatus'] == '1'){
           		 return '否';
           	 }else{
           		 return '未知';
           	 }
            }
         },
        { "data": "operationTime" },
        { "data": "address" },
        {
            render : function(data,type, row, meta) {
           	 if(row['status'] == '0'){
           		 return '已操作';
           	 } else if(row['status'] == '1'){
           		 return '未操作';
           	 }else{
           		 return '未知';
           	 }
            }
         },
        { "data": "productName" },
        { "data": "remark" },
        {
           render : function(data,type, row, meta) {
               return $("#btnGroup").html();
           }
        }
    ],
    "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			$("#edit").validate().resetForm();
		    $("#edit").fill(row);
		    
		    $(".select").trigger("change");
		    $("#productId").val(row.productId);
		    $("#productId").trigger("change");
		    
		 });
		 
		 $(".btn-down").click(function(){
				var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
				swal({
					title : "已操作",
					text : "确定已操作?",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "确定",
					cancelButtonText: "取消",
					closeOnConfirm : false
				},function() {
					$.get(contextpath + "customerOperation/down/" + row.id, function(d){
						$('.datatable-basic').DataTable().draw();
						swal("已操作记录成功","", "success");
					})
				});
			 });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "删除操作记录",
				text : "确定要操作记录?",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "customerOperation/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除操作记录成功","", "success");
				})
			});
		 });
     }
});

$('.all-datatable-basic').DataTable({
	 "ajax" : {
		"url" : contextpath + "customerOperation/list",
		"data" : function(d) {
           var query = $("#query3").serializeObject();
           query["size"] = d.length;
           query["page"] = (d.start/d.length);
			return query;
		},
		"dataFilter": function(data){
           var json = jQuery.parseJSON( data );
           json.recordsTotal = json.totalElements;
           json.recordsFiltered = json.totalElements;
           json.data = json.content;

           return JSON.stringify( json );
       }
	 },
	 "columns": [
        { "data": "customerName" },
        { "data": "distributorName" },
        { "data": "operator" },
        {
            render : function(data,type, row, meta) {
           	 if(row['presentStatus'] == '0'){
           		 return '是';
           	 } else if(row['presentStatus'] == '1'){
           		 return '否';
           	 }else{
           		 return '未知';
           	 }
            }
        },
        { "data": "operationTime" },
        { "data": "address" },
        {
            render : function(data,type, row, meta) {
           	 if(row['status'] == '0'){
           		 return '已操作';
           	 } else if(row['status'] == '1'){
           		 return '未操作';
           	 }else{
           		 return '未知';
           	 }
            }
         },
        { "data": "productName" },
        { "data": "remark" },
        {
            render : function(data,type, row, meta) {
                return $("#btnGroup").html();
            }
         }
    ],
    "drawCallback": function( settings ) {
		 $(".btn-edit").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			$("#edit").validate().resetForm();
		    $("#edit").fill(row);
		    
		    $(".select").trigger("change");
		    $("#productId").val(row.productId);
		    $("#productId").trigger("change");
		    
		    $("#presentStatus").val(row.presentStatus);
 		    $("#presentStatus").trigger("change");
		    
		 });
		 
		 $(".btn-down").click(function(){
				var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
				swal({
					title : "已操作",
					text : "确定已操作?",
					type : "warning",
					showCancelButton : true,
					confirmButtonColor : "#DD6B55",
					confirmButtonText : "确定",
					cancelButtonText: "取消",
					closeOnConfirm : false
				},function() {
					$.get(contextpath + "customerOperation/down/" + row.id, function(d){
						$('.datatable-basic').DataTable().draw();
						swal("已操作记录成功","", "success");
					})
				});
			 });
		 
		 $(".btn-del").click(function(){
			var row = $('.datatable-basic').DataTable().row($(this).closest("tr")).data();
			swal({
				title : "删除操作记录",
				text : "确定要操作记录?",
				type : "warning",
				showCancelButton : true,
				confirmButtonColor : "#DD6B55",
				confirmButtonText : "确定",
				cancelButtonText: "取消",
				closeOnConfirm : false
			},function() {
				$.get(contextpath + "customerOperation/delete/" + row.id, function(d){
					$('.datatable-basic').DataTable().draw();
					swal("删除操作记录成功","", "success");
				})
			});
		 });
     }
});

$(function(){
	$(".btn-add").click(function(){
		$("#edit").validate().resetForm();
		$("#edit")[0].reset();
	});
	
	$("#submitEdit").click(function(){
		if($("#edit").valid()){
			$.post(contextpath + "customerOperation/save", $("#edit").serialize(), function(){
				/*$(":input[name='type']").removeAttr("disabled");
		         $(":input[name='auditStatus']").removeAttr("disabled");
		         $(":input[name='status']").removeAttr("disabled");*/
				$('.datatable-basic').DataTable().draw();
				$('.close').click();
				swal("编辑操作记录成功","", "success");
			})
		}
	});
	 
	$("#submit1").click(function(){
		$('.today-datatable-basic').DataTable().draw();
	});
	
	$("#submit2").click(function(){
		$('.tomorrow-datatable-basic').DataTable().draw();
	});
	
	$("#submit3").click(function(){
		$('.all-datatable-basic').DataTable().draw();
	});

	$('select').select2({
	    minimumResultsForSearch: Infinity
	});	
	$('.js-example-basic-single').select2();


	 $(':input[name="productId"]').on("select2:select",function(){  
			var productName = $(this).select2("data")[0].text;
			$(':input[name="productName"]').val(productName);
	    });
	 
	 
	 $('#operationTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  showDropdowns: true,
				  parentEl: $("#edit_dialog"),
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#operationTime').val("");
	 
	 $('#operationBeginTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  showDropdowns: true,
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#operationBeginTime').val("");
	 
	 $('#operationEndTime').daterangepicker(
			  { 
				  "singleDatePicker": true,
				  showDropdowns: true,
			    locale:{
			    	format: 'YYYY-MM-DD',
		            applyLabel: '确认',
		            cancelLabel: '取消',
		            fromLabel: '从',
		            toLabel: '到',
		            customRangeLabel: '选择时间',
		            daysOfWeek:["日","一","二","三","四","五","六"],
		            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
		        }
			  }
			);
	 $('#operationEndTime').val("");
	 
})
